<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * Ajax Get Node Info action
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 20.05.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * class ajaxGetNodeTree
    ****
    * This class will return node information preformatted
    * by the ajax/nodeinfo.html template.
    ****
    * @package ajax
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 20.05.2005
    * @copyright essentialmind.com 2005
    ***/
    class ajaxGetNodeTree extends ajaxAction{

        function process(){
            $id = (int)$_REQUEST['id'];

            //is this a call for the root node?
            if($id == 1 or $id == 0){
                $id = $this->db->getOne("SELECT id FROM structure WHERE parent_id IS NULL");
            }

            //figure out sorting preferences
            switch($_COOKIE['sorttree']){
                case 'moddate': { $orderBy = "modified"; break;}
                case 'ptype':   { $orderBy = "datatype"; break;}
                case 'publish': { $orderBy = "wf_state"; break;}
                default:        { $orderBy = "title"; break;}
            }

            switch($_COOKIE['sorttreedir']){
                case 'desc':    { $orderBy .= " DESC"; break;}
                default:        { $orderBy .= " ASC"; break;}
            }

            if ($GLOBALS['_CONFIG']['enable_order_position']) {
                //give tree to smarty
                $this->smarty->assign('siteTree',$this->db->getAll("SELECT
                                                                            s.id,
                                                                            s.url,
                                                                            s.level,
    																		s.order_pos,
                                                                            om.title,
                                                                            om.modified,
                                                                            om.wf_state AS wf,
                                                                            d.name AS datatype
                                                                        FROM structure AS s
                                                                        LEFT JOIN objectmap AS om ON (s.id = om.nodeid)
                                                                        LEFT JOIN datatype AS d ON (s.node_type = d.id)
                                                                        WHERE s.parent_id = " . $id . " ORDER BY " . $orderBy));

                //giva page list to smarty
                $this->smarty->assign('sitePages',$this->db->getAll("SELECT
                                                                            p.id,
                                                                            p.pageurl,
                                                                            p.title,
    																		p.order_pos,
                                                                            p.modified,
                                                                            p.wf_state AS wf,
                                                                            d.name AS datatype
                                                                        FROM pagemap AS p
                                                                        LEFT JOIN datatype AS d ON (p.pagetype = d.id)
                                                                        WHERE nodeid = " . $id . " ORDER BY " . $orderBy));
                
            } else {
                //give tree to smarty
                $this->smarty->assign('siteTree',$this->db->getAll("SELECT
                                                                            s.id,
                                                                            s.url,
                                                                            s.level,
                                                                            om.title,
                                                                            om.modified,
                                                                            om.wf_state AS wf,
                                                                            d.name AS datatype
                                                                        FROM structure AS s
                                                                        LEFT JOIN objectmap AS om ON (s.id = om.nodeid)
                                                                        LEFT JOIN datatype AS d ON (s.node_type = d.id)
                                                                        WHERE s.parent_id = " . $id . " ORDER BY " . $orderBy));
            
                //giva page list to smarty
                $this->smarty->assign('sitePages',$this->db->getAll("SELECT
                                                                            p.id,
                                                                            p.pageurl,
                                                                            p.title,
                                                                            p.modified,
                                                                            p.wf_state AS wf,
                                                                            d.name AS datatype
                                                                        FROM pagemap AS p
                                                                        LEFT JOIN datatype AS d ON (p.pagetype = d.id)
                                                                        WHERE nodeid = " . $id . " ORDER BY " . $orderBy));
                
            }
             
            if ($GLOBALS['_CONFIG']['enable_order_position']) {                                          
            	$nextPos = $this->db->getOne("SELECT max(order_pos) FROM pagemap ");
            	$nextPos++;
				
				$this->smarty->assign("nextNodemapValue", $nextPos);
			}
            
            //give page types to smarty
            $this->smarty->assign('pageTypes',$this->db->getAssoc("SELECT id, name FROM datatype WHERE folder = false AND id > 1 AND adminadd = true"));

            //give folder types to smarty
            $this->smarty->assign('folderTypes',$this->db->getAssoc("SELECT id, name FROM datatype WHERE folder = true AND adminadd = true"));

            //give path to node to smarty
            $myTree = new tree($this->db,'structure LEFT JOIN objectmap ON (structure.id = objectmap.nodeid)',null,'id, url, level, title');
            $this->smarty->assign('sitePath',$myTree->getPathStack($id,true));

            //give the id to smarty as well
            $this->smarty->assign('node',$this->db->getRow("SELECT id, parent_id, level, sort_order FROM structure WHERE id = $id"));

            $this->r->html = $this->smarty->preFetch('ajax'.DIRECTORY_SEPARATOR.'sitetree.html');
        }
    }
?>